/**
 * @author 刘超
 * @Note 悬停提示.
 * @UsePage: [资源发现|querylist]
 * @version 1.0
 */
(function (){
	HoverDisplay = function (params){
		this.cname = "HoverDisplay";//			支持参数赋值
		this.minfo = "this";//			支持参数赋值
		this.isAlert = false;//	出错的时候是否有提示		支持参数赋值
		this.selectDisplay = "div"; // 选择用什么来显示 [title | div ]			支持参数赋值
		this.sdValue = {title:"title",div:"div"};

		this.markType = "attr"; // attr | id | ids | name			支持参数赋值
		this.markTag = "div"; // 对象的Tag			支持参数赋值
		this.markId = ""; // 对象的ID			支持参数赋值				2.0中实现
		this.markIds = ""; // 对象的ID集合			支持参数赋值				2.0中实现
		this.markNames = ""; // 对象的Name集合			支持参数赋值				2.0中实现
		this.markName = "isMN"; // 用来标记某个元素要添加悬停			支持参数赋值
		this.markValue = "mv"; // 标记的值,用于区分标记.			支持参数赋值
		this.paramsIsLoaded = false; // 标志着参数是否加载完成

		this.div = {}; // div 对象
		this.divAG = "blue"; // 前景色			支持参数赋值
		this.divBG = "white"; // 背景色			支持参数赋值

		this.targetFind = "";   // find方法		支持参数赋值
		this.timesFind = 100;  // find方法		支持参数赋值
		this.intervalFind = 10;  // find方法		支持参数赋值
		this.fnFind = function (){};  // find方法		支持参数赋值

		if(params){ // 当参数不为空的时候就加载参数
			if((typeof params=='object') && params.constructor==Object){
	            this.loadParams(params);
	        }else{
	        	return false; // 表示用户要自己调用方法
	        }
		}
		this.createDiv(this.divAG, this.divBG);
		this.find(this.targetFind, this.timesFind, this.intervalFind, this.fnFind);

	}
	HoverDisplay.prototype.loadParams = function (ps){
		this.minfo = "loadParams";

		if(ps){
			for(var key in ps){
				this[key] = ps[key];
			}
			this.paramsIsLoaded = true;
		}
	}
	HoverDisplay.prototype.createDiv = function (ag,bg){
		this.minfo = "createDiv";

		this.find(function (){
			return document.body ? true : false;
		}, this.timesFind, this.intervalFind, function (hd){
			var div = document.createElement("div");
			div.style.width = "200";
//			div.style.height = "10";
			div.style.position = "absolute";
			div.style.border = "1px solid gray";
			div.style.background = bg;
			div.style.color = ag;
			div.style.zIndex = 100;
			div.style.wordWrap = "break-word"; // > 自动换行
			div.style.wordBreak = "normal";    // >
			div.style.display = "none";
			document.body.appendChild(div);

			hd.div = div;
		});
	}
	/**
	 * 查看某个对象是否存在,存在的话就执行某个函数
	 * target : 对象[ID|自定义函数]
	 * times : 寻找多杀次
	 * interval : 时间间隔
	 * fn : function 函数
	 */
	HoverDisplay.prototype.find = function (target,times,interval,fn){
		this.minfo = "find";

		var this_ = this;
		var args = arguments;
		var cycles = 0;
		var ttObj = null;

		if(args.length > 0){
			if((typeof target=='string') && target.constructor==String){
				ttObj = function (){
					return document.getElementById(target);
				}
	        }else if((typeof target=='function') && target.constructor==Function){
	        	ttObj = target;
	        }else {
	        	this_.information("参数[target]不符合要求!");
	        	return false;
	        }
		}else{
			return false;
		}
		function start(){
			if(ttObj()){
				fn(this_);
			}else{
				if(cycles >= times){
					this_.information("没有找到该对象!");
					return false;
				}
				cycles++;
				setTimeout(start,interval);
			}
		}
		start();
	}
	HoverDisplay.prototype.bindHD = function (advance){
		this.minfo = "bindHD";

		var this_ = this;

		function attrFn(domTag,mName,mValue){  // 当用选择使用属性来加载悬停
			var objs = document.getElementsByTagName("div");
			for(var i=0; i<objs.length; i++){
				var obj = objs[i];
				var isMark = obj.getAttribute(mName);
				if(isMark == mValue){
					if(advance){ // 预前处理方法
						advance(obj);
					}
					selectDisplayFn(obj);
				}
			}
		}

		function selectDisplayFn(currDom){
			if(this_.selectDisplay == this_.sdValue.title){
				currDom.title = currDom.innerHTML;
			}else if(this_.selectDisplay == this_.sdValue.div){
				currDom.onmouseover = function (){
					if(!this.innerHTML){
						return false;
					}
					var x = event.x + document.body.scrollLeft;
				    var y = event.y + document.body.scrollTop + 20;
				    this_.div.style.left = x;
					this_.div.style.top = y;
					this_.div.style.display = "";
					this_.div.innerHTML = this.innerHTML;

				};
				currDom.onmouseout = function (){
					this_.div.innerHTML = "";
					this_.div.style.display = "none";
				};
				currDom.onmousemove = function (){
					var x = event.x + document.body.scrollLeft;
				    var y = event.y + document.body.scrollTop + 20;
				    this_.div.style.left = x;
					this_.div.style.top = y;
				};
			}else{
				return "";
			}
		}
		if(this.markType.indexOf("attr") > -1){
			attrFn(this.markTag,this.markName,this.markValue);
		}else if(this.markType.indexOf("ids") > -1){

		}else if(this.markType.indexOf("id") > -1){

		}else if(this.markType.indexOf("name") > -1){

		}
	}
	/**
	 * 自定义封装提示消息
	 * isAlert 为 true 的时候开启
	 */
	HoverDisplay.prototype.information = function (content){
		if(this.isAlert){
			var info = "";
			info += "来自";
			info += this.cname;
			info += ".";
			info += this.minfo;
			info += "方法的提示消息:\n";
			info += "======[start]========\n";
			info += content;
			info += "\n=======[end]=========";
			alert(info);
		}
	}
})();